<?php

namespace App\Http\Controllers\Wechat\Plugins;

use Carbon\Carbon;
use Illuminate\Support\Facades\Cache;
use Illuminate\Http\Request;
use Laravel\Lumen\Routing\Controller as BaseController;
use App\Http\Controllers\Wechat;
use Xu42\Qznjw2014\Qznjw2014 as EducationSystem;
use Xu42\DlpuEcard\DlpuEcard;


class Education extends BaseController
{

    /**
     * 当前学期
     * @var string
     */
    private static $previousSemester = '2016-2017-1';
    private static $currentSemester  = '2016-2017-2';


    public static function index($message, $oper = '')
    {
        Cache::flush();
        $cacheKey = 'user_' . $message->FromUserName;

        // 因缓存问题，新绑定用户在三个小时内无法使用查询功能 修复该bug
        if (Cache::get($cacheKey)) {
            $user = unserialize(Cache::get($cacheKey));
        } else {
            $user = \App\Library\User::isRegisterByOpenid($message->FromUserName);
            if ($user) {
                Cache::put($cacheKey, serialize($user), Carbon::now()->addDay());
            }
        }

        if (!$user) {
            return Wechat\Messages\News::register($message);
        }

        if ($oper == 'scoreFinal') {
            return self::scoreFinal($message, $user);
        }

        if ($oper == 'levelScores') {
            return self::levelScores($message, $user);
        }

        if ($oper == 'timetable') {
            return self::timetable($message, $user);
        }

        if ($oper == 'scoreRebuilt') {
            return self::scoreRebuilt($message, $user);
        }

        if ($oper == 'book') {
            return self::book($message, $user);
        }

        if ($oper == 'news') {
            return self::news($message, $user);
        }

        if ($oper == 'ecard') {
            return self::ecard($message, $user);
        }

        if ($oper == 'examsInfo') {
            return self::examsinfo($message, $user);
        }

        return null;
    }

    /**
     * 学期成绩
     * @param $message
     * @return \EasyWeChat\Message\News|\EasyWeChat\Message\Text
     */
    public static function scoreFinal($message, $user)
    {
        $eduSystem = new EducationSystem($user->username, base64_decode($user->password));

        $scoreFinal = $eduSystem->coursesScores(self::$currentSemester);

        return Wechat\Messages\News::scoreFinal($message->FromUserName, $scoreFinal);
    }

    /**
     * 等级成绩
     * @param $message
     * @return array
     */
    public static function levelScores($message, $user)
    {
        $eduSystem = new EducationSystem($user->username, base64_decode($user->password));

        $levelScores = $eduSystem->levelScores();

        return Wechat\Messages\News::levelScores($message->FromUserName, $levelScores);
    }


    /**
     * 四六级成绩
     * @param $message
     * @return array
     */
    public static function scoreCet($message)
    {
        return Wechat\Messages\News::scoreCet();
    }


    /**
     * 单日课表
     *
     * 记录一个坑。。。
     * 微信开发, 一定要不显示notice!! 用@抑制notice
     *
     * @param $message
     * @param $user
     * @return array
     */
    public static function timetable($message, $user)
    {
        $eduSystem = new EducationSystem($user->username, base64_decode($user->password));

        $currentWeek = $eduSystem->currentWeek();

        $timetable = @$eduSystem->timetable(self::$currentSemester, $currentWeek);

        return Wechat\Messages\News::timetable($message->FromUserName, $timetable);
    }


    /**
     * 重修成绩
     * @param $message
     * @return \EasyWeChat\Message\Text
     */
    public static function scoreRebuilt($message, $user)
    {
        return Wechat\Messages\Text::simple('您已注册，敬请期待');
    }

    /**
     * 图书查询
     * @param $message
     * @return \EasyWeChat\Message\Text
     */
    public static function book($message, $user)
    {
        return Wechat\Messages\Text::simple('您已注册，敬请期待');
    }

    /**
     * 新闻动态
     * @param $message
     * @return \EasyWeChat\Message\Text
     */
    public static function news($message, $user)
    {
        return Wechat\Messages\Text::simple('您已注册，敬请期待');
    }


    /**
     * 成绩 Html
     * @param Request $request
     * @param $type
     * @param $openId
     */
    public function scoresHtml(Request $request, $type, $openId)
    {
        $user = \App\Library\User::isRegisterByOpenid($openId);

        $scores = [];

        // 未注册用户
        if (!$user) {
            return view('wechat.scores')->with('data', $scores);
        }

        $eduSystem = new EducationSystem($user->username, base64_decode($user->password));

        if ($type == 'all') {
            $scores['title']    = $user->realname . '的大学成绩单';
            $scores['semester'] = '大学至今所有成绩';
            $scores['scores']   = $eduSystem->coursesScores();
        } else {
            $scores['title']    = $user->realname . '的期末成绩单';
            $scores['semester'] = '学期：' . self::$currentSemester;
            $scores['scores']   = $eduSystem->coursesScores(self::$currentSemester);
        }

        $app      = new \EasyWeChat\Foundation\Application(config('wechat'));
        $jsConfig = $app->js->config([
            'onMenuShareTimeline',
            'onMenuShareAppMessage',
            'onMenuShareQQ',
            'onMenuShareQZone'
        ], false);

        return view('wechat.scores')->with('datas', $scores)->with('jsconfig', $jsConfig);
    }


    /**
     * 校园卡信息查询
     * @param $message
     * @param $user
     * @return \EasyWeChat\Message\Text
     */
    public static function ecard($message, $user)
    {
        $dlpuEcard        = new DlpuEcard($user->username);
        $ecardBalance     = $dlpuEcard->getBalance();
        $ecardSumconsumer = $dlpuEcard->getSumconsumer();

// '学号：'. $user->username  . "\n" . '校园卡余额:' . $ecardBalance['data']['oddfare'] . "\n" . '消费总额：' . $ecardSumconsumer
        return Wechat\Messages\Text::simple('学号：' . $user->username . "\n" . '校园卡余额：' . $ecardBalance['data']['oddfare']);
    }


    /**
     * 考试安排信息
     *
     * @param $message
     * @param $user
     * @return array
     */
    public static function examsinfo($message, $user)
    {
        $eduSystem = new EducationSystem($user->username, base64_decode($user->password));

        $examsinfo = @$eduSystem->examsinfo(self::$currentSemester);

        return Wechat\Messages\News::examsinfo($message->FromUserName, $examsinfo);
    }

}
